package top.infopub.mgr.cap.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.cap.ContractReviewDto;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.cap.dao.ContractReviewDao;
import top.infopub.mgr.cap.domain.ContractReviewDomain;
import top.infopub.mgr.cap.jsonvo.ContractReviewJsonVo;
import top.infopub.mgr.cap.service.CapProcessManageService;
import top.infopub.mgr.cap.service.ContractReviewService;
import top.infopub.mgr.cap.vo.ContractReviewVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.SetupProService;
import top.infopub.model.cap.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;

import java.util.Date;
import java.util.List;

@Service
public class ContractReviewServiceImpl  extends GenericServiceImpl<ContractReview, String> implements ContractReviewService {

    @Autowired
    private ContractReviewDao contractReviewDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private SetupProService setupProService;

    @Autowired
    private OAFlowService oaFlowService;

    @Autowired
    private CapProcessManageService capProcessManageService;

    @Override
    public GenericDao<ContractReview, String> getDao() {
        return contractReviewDao;
    }

    @Override
    public Page<ContractReviewDomain> selectByDomainWhere(Page<ContractReviewDomain> page, ContractReviewVo contractReviewVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ContractReviewDomain>)contractReviewDao.selectByDomainWhere(contractReviewVo);
    }

    @Override
    public ContractReviewJsonVo queryDetailById(String id) {
        ContractReviewJsonVo jsonVo=new ContractReviewJsonVo();
        /*融资基础信息*/
        ContractReviewDto dto=contractReviewDao.queryById(id);
        jsonVo.setConReviewDto(dto);
        /*借款合同信息*/
        List<ContractReviewLoad> conLoadDetails=contractReviewDao.queryConLoanDetails(dto.getProCode());
        jsonVo.setConLoadDetails(conLoadDetails);
        /*保证合同信息*/
        List<ContractGuarantyDetail> conGuaDetails =contractReviewDao.queryConGuaDetails(dto.getProCode());
        jsonVo.setConGuaDetails(conGuaDetails);
        /*质押合同*/
        List<ContractPledgeDetail>  conPleDetails=contractReviewDao.queryConPleDetails(dto.getProCode());
        jsonVo.setConPleDetails(conPleDetails);
         /*抵押合同信息*/
        List<ContractMortgageDetail> conMorDetails=contractReviewDao.queryConMorDetails(dto.getProCode());
        jsonVo.setConMorDetails(conMorDetails);
        /*附件信息表*/
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(dto.getProCode(), ProjectFileTypeEnum.FILE_TYPE52.getCode(), null);
        jsonVo.setFileAtt(setupProService.fileAttaListToArray(projectAttaList));
        return jsonVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RspVo edit(ContractReviewDto contractReviewDto, List<ProjectAttachment> attachmentList,
                      Integer submitType, AuthorizationVO vo) {
        //校验数据是否存在
        ContractReview tmpPi = contractReviewDao.selectByPrimaryKey(contractReviewDto.getId());
        if (tmpPi == null) return RspVo.getFailureResponse(400, "合同评审数据不存在");
        //判断审批状态
        if (org.apache.commons.lang.StringUtils.isNotBlank(tmpPi.getProcessStatus())
                && (org.apache.commons.lang.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), tmpPi.getProcessStatus())
                || org.apache.commons.lang.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), tmpPi.getProcessStatus()))) {
            return RspVo.getFailureResponse(400, "审批状态不正确，操作失败");
        }
        ContractReview contractReview = new ContractReview();
        contractReview.setId(tmpPi.getId());
        contractReview.setUptBy(vo.getId());
        contractReview.setUptTime(new Date());
        contractReview.setProCode(contractReviewDto.getProCode());
        contractReview.setContractSpecification(contractReviewDto.getContractSpecification());
        //更新主表信息
        contractReviewDao.updateByIdSelective(contractReview);

        //更新附件信息
        projectAttachmentDao.deleteByWhere(tmpPi.getProCode(), ProjectFileTypeEnum.FILE_TYPE52.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        if (submitType == 1) {
            if (org.apache.commons.lang.StringUtils.isNotBlank(tmpPi.getOaId())) {
                oaFlowService.finishOA(tmpPi.getOaId());
            }
            ContractReviewJsonVo payAppJson = queryDetailById(tmpPi.getId());
            String detailJson= JSON.toJSONString(payAppJson);
            capProcessManageService.sendProcess(CapProcessDetailTypeEnum.PRO_NODE_TYPE13, detailJson, contractReview.getId(), contractReview.getProCode(), vo);
        }
        return RspVo.getSuccessResponse();
    }

}
